const router = require('koa-router')()
const { models } = require('../../model')
const UserController = require('../../controller/user')
const { ObjectID } = require('mongodb')
const userController = new UserController()
const { initSchedules } = require('../../service/msg/schedule')
router.use(userController.userFilter())

// 公共可用列表
router.get('/list/pub', async function (ctx) {
  const findQuery = {
    status: 1
  }
  const docs = await models.Tophub
      .find(findQuery)
      .sort({ platform: 1, col: 1 })
      .project({})
      .toArray()
  ctx.wrapper.succ(docs)
})

// 用户列表
router.get('/list/user', async function (ctx) {
  const findQuery = {}
  findQuery.createUserId = ctx._user._id
  findQuery.status = {
    $lt: 3
  }
  const docs = await models.UserTophub
      .find(findQuery)
      .project({})
      .toArray()
  const hubs = await models.Tophub
      .find({})
      .project({})
      .toArray()
  const channels = await models.MsgChannel
      .find({})
      .project({})
      .toArray()
  docs.forEach(doc => {
    doc.hubs = []
    doc.hubIds.forEach(h => {
      doc.hubs.push(hubs.filter(e => e._id.toString() === h)[0])
    })
    doc.channels = []
    doc.channelIds.forEach(c => {
      doc.channels.push(channels.filter(e => e._id.toString() === c)[0])
    })
  })
  ctx.wrapper.succ(docs)
})

router.post('/create', async function (ctx) {
  if (!ctx._user) return ctx.wrapper.console.error('PERMISSION_ERROR', '未登录')
  const body = ctx.request.body
  const doc = {
    type: body.type,
    rules: body.rules,
    hubIds: body.hubIds,
    channelIds: body.channelIds,
    status: 1,
    createUserId: ctx._user._id
  }
  if (body.type === 2) {
    // icon: body.platform,
    doc.name = body.name
  }
  const ret = await models.UserTophub.insertOne(doc)
  initSchedules().then((msg) => {
    console.log(msg)
  })
  return ctx.wrapper.succ({ _id: ret.insertedId })
})

router.post('/update', async function (ctx) {
  if (!ctx._user) return ctx.wrapper.console.error('PERMISSION_ERROR', '未登录')
  const body = ctx.request.body
  const ret = await models.UserTophub.updateOne({
    _id: ObjectID(body._id)
  }, {
    $set: {
      rules: body.rules,
      hubIds: body.hubIds,
      channelIds: body.channelIds
    }
  }
  )
  initSchedules().then((msg) => {
    console.log(msg)
  })
  return ctx.wrapper.succ(ret)
})

router.post('/pause', async function (ctx) {
  if (!ctx._user) return ctx.wrapper.console.error('PERMISSION_ERROR', '未登录')
  const ret = await models.Tophub.updateOne({
    _id: ObjectID(ctx.request.body.id)
  }, {
    $set: { status: ctx.request.body.status }
  }, { multi: false })
  return ctx.wrapper.succ(ret)
})

router.post('/delete', async function (ctx) {
  if (!ctx._user) return ctx.wrapper.console.error('PERMISSION_ERROR', '未登录')
  const id = ctx.request.body.id
  const ret = await models.Tophub.updateOne({
    _id: ObjectID(id)
  }, {
    $set: { status: 3 }
  }, { multi: false })
  return ctx.wrapper.succ(ret)
})

module.exports = router
